package com.meida.module.system.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.model.SaveModel;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.utils.FlymeUtils;
import com.meida.common.utils.JsonUtils;
import com.meida.module.system.client.entity.BaseConfig;
import com.meida.module.system.provider.mapper.BaseConfigMapper;
import com.meida.module.system.provider.service.BaseConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统配置表 服务实现类
 *
 * @author flyme
 * @date 2019-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class BaseConfigServiceImpl extends BaseServiceImpl<BaseConfigMapper, BaseConfig> implements BaseConfigService {

    @Override
    public void initBaseConfig() {
        Map<String, Object> configMaps = new HashMap<>(0);
        List<BaseConfig> configs = list();
        if (ObjectUtils.isNotEmpty(configs)) {
            redisUtils.del("CONFIG_MAPS");
            configs.forEach(config -> {
                String key = config.getConfigKey();
                configMaps.put(key, config.getConfigVal());

            });
            redisUtils.setMap("CONFIG_MAPS", configMaps);
            log.info("初始化系统配置完成####################");
        }
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<BaseConfig> listByType(String configType) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq(true, "configType", configType);
        return list(qw);
    }
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public BaseConfig getByKey(String configKey) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq(true, "configKey", configKey);
        return getOne(qw);
    }

    /**
     * 保存对象存储配置
     *
     * @param model
     * @return
     */
    @Override
    public ResultBody saveOss(SaveModel model) {
        CriteriaSave cs = new CriteriaSave(model, entityClass);
        String serviceName = cs.getParams("serviceName");
        String filePath = cs.getParams("filePath");
        String http = cs.getParams("http");
        String endpoint = cs.getParams("endpoint");
        String bucket = cs.getParams("bucket");
        String secretKey = cs.getParams("secretKey");
        String accessKey = cs.getParams("accessKey");

        BaseConfig used = getByKey("OSS_USED");
        if (FlymeUtils.isEmpty(used)) {
            used = new BaseConfig();
            used.setConfigKey("OSS_USED");
            used.setConfigType("oss");
            used.setRemark("OSS存储方式");
        }
        used.setConfigVal(serviceName);
        saveOrUpdate(used);


        BaseConfig oss = getByKey(serviceName);
        if (FlymeUtils.isEmpty(oss)) {
            oss = new BaseConfig();
            oss.setConfigKey(serviceName);
            oss.setConfigType("oss");
        }

        EntityMap map = new EntityMap();
        map.put("filePath", filePath);
        map.put("http", http);
        map.put("changed", true);
        map.put("serviceName", serviceName);
        map.put("endpoint", endpoint);
        if (serviceName.equals("ALI_OSS")) {
            oss.setRemark("阿里云存储配置");
            map.put("bucket", bucket);
            map.put("secretKey", secretKey);
            map.put("accessKey", accessKey);
        }
        if (serviceName.equals("LOCAL_OSS")) {
            oss.setRemark("本地存储配置");
        }
        oss.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(oss);
        //刷新缓存
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }

    @Override
    public ResultBody saveSms(SaveModel model) {
        CriteriaSave cs = new CriteriaSave(model, entityClass);
        Boolean enable = cs.getParams("enable");
        Boolean encryption = cs.getParams("encryption");
        String smsCodeHander = cs.getParams("smsCodeHander");
        EntityMap map = new EntityMap();
        map.put("enable", enable);
        map.put("encryption", encryption);
        map.put("smsCodeHander", smsCodeHander);
        BaseConfig sms = getByKey("SMS_CONFIG");
        if (FlymeUtils.isEmpty(sms)) {
            sms = new BaseConfig();
            sms.setConfigKey("SMS_CONFIG");
            sms.setConfigType("sms");
        }
        sms.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(sms);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }

    @Override
    public ResultBody savePush(SaveModel model) {
        CriteriaSave cs = new CriteriaSave(model, entityClass);
        String serviceName = cs.getParams("serviceName");
        String appKey = cs.getParams("appKey");
        String secretKey = cs.getParams("secretKey");
        EntityMap map = new EntityMap();
        map.put("serviceName", serviceName);
        map.put("appKey", appKey);
        map.put("secretKey", secretKey);
        BaseConfig push = getByKey("PUSH_CONFIG");
        if (FlymeUtils.isEmpty(push)) {
            push = new BaseConfig();
            push.setConfigKey("PUSH_CONFIG");
            push.setConfigType("push");
            push.setRemark("推送配置");
        }
        push.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(push);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }
    @Override
    public ResultBody saveIm(Map params) {
        CriteriaSave cs = new CriteriaSave(params, entityClass);
        String serviceName = cs.getParams("serviceName");
        String appKey = cs.getParams("appKey");
        String appSecret = cs.getParams("appSecret");
        EntityMap map = new EntityMap();
        map.put("serviceName", serviceName);
        map.put("appKey", appKey);
        map.put("appSecret", appSecret);
        BaseConfig config = getByKey("IM_CONFIG");
        if (FlymeUtils.isEmpty(config)) {
            config = new BaseConfig();
            config.setConfigKey("IM_CONFIG");
            config.setConfigType("im");
            config.setRemark("即时通讯配置");
        }
        config.setConfigVal(JsonUtils.beanToJson(map));
        saveOrUpdate(config);
        initBaseConfig();
        return ResultBody.ok("设置成功", true);
    }
}
